import { Request, Response, NextFunction } from 'express';
import { AppError } from './errorHandler';

export const validateCirclePost = (req: Request, res: Response, next: NextFunction) => {
    const { content, circleId } = req.body;

    if (!content || typeof content !== 'string') {
        return next(new AppError('帖子内容不能为空', 400));
    }

    if (content.length > 1000) {
        return next(new AppError('帖子内容不能超过1000个字符', 400));
    }

    if (!circleId || typeof circleId !== 'number') {
        return next(new AppError('圈子ID不能为空', 400));
    }

    // 验证图片数组（如果存在）
    if (req.body.images) {
        if (!Array.isArray(req.body.images)) {
            return next(new AppError('图片必须是数组格式', 400));
        }

        if (req.body.images.length > 9) {
            return next(new AppError('最多只能上传9张图片', 400));
        }

        for (const image of req.body.images) {
            if (typeof image !== 'string') {
                return next(new AppError('图片URL必须是字符串格式', 400));
            }
        }
    }

    next();
};

export const validateCircle = (req: Request, res: Response, next: NextFunction) => {
    const { name, description, avatar } = req.body;

    if (!name || typeof name !== 'string') {
        return next(new AppError('圈子名称不能为空', 400));
    }

    if (name.length < 2 || name.length > 20) {
        return next(new AppError('圈子名称长度必须在2-20个字符之间', 400));
    }

    if (description && typeof description !== 'string') {
        return next(new AppError('圈子描述必须是字符串格式', 400));
    }

    if (description && description.length > 200) {
        return next(new AppError('圈子描述不能超过200个字符', 400));
    }

    if (avatar && typeof avatar !== 'string') {
        return next(new AppError('圈子头像必须是字符串格式', 400));
    }

    next();
}; 